package com.sbr.seb.reader;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import com.sbr.seb.entity.TomcatApplication;


public class ManagerListReader {

	public List<TomcatApplication> read(BufferedReader in) {
		String inputLine = null;
		List<TomcatApplication> applicationList = new ArrayList<TomcatApplication>();
		try {
			in.readLine();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} //FIRST line is garbage
		try {
			while ((inputLine = in.readLine()) != null) {
				applicationList.add(readLine(inputLine));
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return applicationList;
	}
	
	/**
	 * OK - Applications listï¿½es pour l'hï¿½te virtuel (virtual host) localhost
		/:running:0:ROOT
		/manager:running:1:manager
		/hudson:running:0:hudson
		/docs:running:0:docs
		/examples:running:0:examples
		/host-manager:running:0:host-manager
		
	 * @param line
	 */
	
	private TomcatApplication readLine(String line) {
		System.out.println(line);

		StringTokenizer st = new StringTokenizer(line,":");
		String url = st.nextToken();
		String state = st.nextToken();
		String nbSession = st.nextToken();
		String name = st.nextToken();
		
		TomcatApplication application = new TomcatApplication();
		application.setUrl(url);
		application.setState(state);
		application.setNbSession(nbSession);
		application.setName(name);
		
		return application;
		
	}
	
}
